local bolan = fk.CreateSkill{
    name = "va_heg__bolan",
    attached_skill_name = "va_heg__bolan_other&",
}

Fk:loadTranslationTable{
    ["va_heg__bolan"] = "博览",
    [":va_heg__bolan"] = "出牌阶段开始时，你可以观看牌堆顶三张牌并可以使用其中一张牌；"..
    "其他角色的出牌阶段限一次，其可以令你观看至多三张手牌，然后你可以使用其中一张牌并令此技能失效直至你下回合开始。",

    ["#va_heg__bolan_active"] = "博览",
    ["#va_heg__bolan-use"] = "博览：使用其中一张牌",
    ["@@va_heg__bolan_prohibit"] = "博览 禁止使用",

    ["$va_heg__bolan1"] = "博览群书，融会贯通。",
    ["$va_heg__bolan2"] = "博览于文，约之以礼。",
}

local H = require "packages/ol_hegemony/util"

bolan:addEffect(fk.EventPhaseStart,{
    anim_type = "support",
    can_trigger = function (self, event, target, player, data)
        return target == player and player:hasSkill(bolan.name) and player.phase == Player.Play
    end,
    on_use = function (self, event, target, player, data)
        local room = player.room
        local result, dat = room:askToUseActiveSkill(player, {
            skill_name = "#va_heg__bolan_active",
            prompt = "#va_heg__bolan-use",
            cancelable = true,
        })
        if result and dat then
            room:useCard({
                card = Fk:getCardById(dat.cards[1]),
                from = player,
                tos = dat.targets,
                extraUse = true,
            })
        end
    end,
})

local bolan_spec = {
    can_refresh = function(self, event, target, player, data)
        return target == player
      end,
    on_refresh = function(self, event, target, player, data)
        local room = player.room
        local players = room.alive_players
        local zhongyans = table.filter(players, function(p) return p:hasShownSkill(bolan.name) end)
        local bolan_map = {}
        for _, p in ipairs(players) do
          local will_attach = false
          for _, zhongyan in ipairs(zhongyans) do
            if (zhongyan ~= p ) then
              will_attach = true
              break
            end
          end
          bolan_map[p] = will_attach
        end
        for p, v in pairs(bolan_map) do
          if v ~= p:hasSkill("va_heg__bolan_other&") then
            room:handleAddLoseSkills(p, v and bolan.attached_skill_name or "-" .. bolan.attached_skill_name, nil, false, true)
          end
        end
      end,
}

bolan:addEffect(fk.AfterPropertyChange, bolan_spec)
bolan:addEffect(fk.GeneralRevealed, bolan_spec)
bolan:addEffect(fk.GeneralHidden, bolan_spec)
bolan:addEffect(fk.Deathed, bolan_spec)

local addLoseEffect = function (self, player, _)
  local room = player.room
  for _, p in ipairs(room.alive_players) do
    if p ~= player then
      room:handleAddLoseSkills(p, bolan.attached_skill_name, nil, false, true)
    end
  end
end
bolan:addAcquireEffect(addLoseEffect)
bolan:addLoseEffect(addLoseEffect)

bolan:addEffect(fk.TurnStart,{
    late_refresh = true,
    can_refresh = function (self, event, target, player, data)
        return target == player and player:getMark("@@va_heg__bolan_prohibit") > 0
    end,
    on_refresh = function (self, event, target, player, data)
        player.room:setPlayerMark(player, "@@va_heg__bolan_prohibit", 0)
    end,
})

return bolan